package com.zxl.controller;

import com.zxl.feign.UserFeign;
import com.zxl.vo.R;
import com.zxl.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@RequestMapping("/user")
@Controller
public class Usercontroller {

    @Autowired
    private UserFeign userFeign;

    @PostMapping("/add")
    @ResponseBody
    public R add(User user) {
        boolean add = userFeign.add(user);
        return R.write(add);
    }

    @RequestMapping("/{page}.html")
    public String toPage(@PathVariable("page") String page){
        return "user/"+page;
    }

    @GetMapping("/list")
    public String list(Model model){
        List<User> user = userFeign.getUser();
        model.addAttribute("users",user);
        return "user/userList";
    }

    @PostMapping("/delete")
    public R delete(Integer[] ids){
        boolean delete = userFeign.delete(ids);
        return R.write(delete);
    }

    @RequestMapping("/toupdate")
    public String toUpdate(Integer id,Model model){
        User user = userFeign.getById(id);
        model.addAttribute("user",user);
        return "user/editUser";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public R edit(User user){
        Boolean edit = userFeign.edit(user);
        return R.write(edit);
    }

    @RequestMapping("/findBySome")
    public String findBySome(User user,Model model){
        List<User> users = userFeign.findBySome(user);
        model.addAttribute("user",user);
        model.addAttribute("users",users);
        return "user/userList";
    }

}
